import { undoManager, checkSelection } from "../util/Utils.js";
import $ from "jquery";

export default {
  // 左右对齐
  textAlign (designer, align) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    let oldAligns = this.builderTextAlign(context, align);
    undoManager.add({
      undo: function () {
        oldAligns = this.builderTextAlign(context, null, oldAligns);
        context.isModify = true;
      },
      redo: function () {
        oldAligns = this.builderTextAlign(context, align);
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  // 构建左右对齐
  builderTextAlign (context, align, prevAligns) {
    const oldAligns = {};
    const selected = context.hot.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        const td = context.hot.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldAligns[i + "," + j] = cellStyle.align || "";
        if (prevAligns) {
          align = prevAligns[i + "," + j];
        }
        $(td).css("text-align", align);
        cellStyle.align = align;
        this.textAlignValue = align;
      }
    }
    return oldAligns;
  },
  // 上下对齐
  verticalAlign (designer, align) {
    const { context } = designer;
    if (!checkSelection(context)) {
      return;
    }
    let oldAligns = this.builderVerticalAlign(context, align);
    undoManager.add({
      undo: function () {
        oldAligns = this.builderVerticalAlign(context, null, oldAligns);
        context.isModify = true;
      },
      redo: function () {
        oldAligns = this.builderVerticalAlign(context, align);
        context.isModify = true;
      }
    });
    context.isModify = true;
  },
  // 构建上下对齐
  builderVerticalAlign (context, valign, prevAligns) {
    const oldAligns = {};
    const selected = context.hot.getSelected()[0];
    let startRow = selected[0];
    let startCol = selected[1];
    let endRow = selected[2];
    let endCol = selected[3];
    let tmp = endRow;
    if (startRow > endRow) {
      endRow = startRow;
      startRow = tmp;
    }
    tmp = endCol;
    if (startCol > endCol) {
      endCol = startCol;
      startCol = tmp;
    }
    for (let i = startRow; i <= endRow; i++) {
      for (let j = startCol; j <= endCol; j++) {
        const cellDef = context.getCell(i, j);
        const td = context.hot.getCell(i, j);
        if (!cellDef) {
          continue;
        }
        const cellStyle = cellDef.cellStyle;
        oldAligns[i + "," + j] = cellStyle.valign || "";
        if (prevAligns) {
          valign = prevAligns[i + "," + j];
        }
        $(td).css("vertical-align", valign);
        cellStyle.valign = valign;
        this.verticalAlignValue = valign;
      }
    }
    return oldAligns;
  }
};
